#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-03-14 18:06:15
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import smtplib  
from email.mime.text import MIMEText  
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

mailto_list = ['13472843610@139.com',] 
mail_host = 'smtp.163.com'  #设置服务器
mail_user = 'hero007asd'    #用户名
mail_pass = 'hero007asd@163'  #口令 
mail_postfix = '163.com'  #发件箱的后缀

def send_mail(to_list,sub,content):
    '''
    发送txt文本邮件
    小五义：http://www.cnblogs.com/xiaowuyi
    '''
    me="<"+mail_user+"@"+mail_postfix+">"  
    msg = MIMEText(content,_subtype='plain',_charset='utf8')  
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP()  
        server.connect(mail_host)  
        server.login(mail_user,mail_pass)  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception, e:
        print str(e)  
        return False

if __name__ == '__main__':
    if send_mail(mailto_list,"欧元/美元当前警示价","hello world！"):
        print "发送成功"
    else:
        print "发送失败"

def send_html_mail(to_list,sub,content):  #to_list：收件人；sub：主题；content：邮件内容
    '''
    发送html文本邮件
    小五义：http://www.cnblogs.com/xiaowuyi
    '''
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"   #这里的hello可以任意设置，收到信后，将按照设置显示
    msg = MIMEText(content,_subtype='html',_charset='utf8')    #创建一个实例，这里设置为html格式邮件
    msg['Subject'] = sub    #设置主题
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        s = smtplib.SMTP()  
        s.connect(mail_host)  #连接smtp服务器
        s.login(mail_user,mail_pass)  #登陆服务器
        s.sendmail(me, to_list, msg.as_string())  #发送邮件
        s.close()  
        return True  
    except Exception, e:  
        print str(e)  
        return False  
# if __name__ == '__main__':  
#     if send_mail(mailto_list,"hello","<a href='http://www.cnblogs.com/xiaowuyi'>小五义</a>"):  
#         print "发送成功"  
#     else:  
#         print "发送失败"  



def sendAccessory():
    '''
    发送带附件邮件
    小五义：http://www.cnblogs.com/xiaowuyi
    '''
    #创建一个带附件的实例
    msg = MIMEMultipart()

    #构造附件1
    att1 = MIMEText(open('d:\\123.rar', 'rb').read(), 'base64', 'utf8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="123.doc"'#这里的filename可以任意写，写什么名字，邮件中显示什么名字
    msg.attach(att1)

    #构造附件2
    att2 = MIMEText(open('d:\\123.txt', 'rb').read(), 'base64', 'utf8')
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename="123.txt"'
    msg.attach(att2)

    #加邮件头
    msg['to'] = 'YYY@YYY.com'
    msg['from'] = 'XXX@XXX.com'
    msg['subject'] = 'hello world'
    #发送邮件
    try:
        server = smtplib.SMTP()
        server.connect('smtp.XXX.com')
        server.login('XXX','XXXXX')#XXX为用户名，XXXXX为密码
        server.sendmail(msg['from'], msg['to'],msg.as_string())
        server.quit()
        print '发送成功'
    except Exception, e:  
        print str(e) 

def AutoSendMail():
    '''
    利用MIMEimage发送图片
    '''
    msg = MIMEMultipart()
    msg['From'] = "XXX@XXX.com"
    msg['To'] = "YYY@YYY.com"
    msg['Subject'] = "hello world"


    txt = MIMEText("这是中文的邮件内容哦",'plain','utf8')     
    msg.attach(txt)
    

    file1 = "C:\\hello.jpg"
    image = MIMEImage(open(file1,'rb').read())
    image.add_header('Content-ID','<image1>')
    msg.attach(image)
    
    
    server = smtplib.SMTP()
    server.connect('smtp.XXX.com')
    server.login('XXX','XXXXXX')
    server.sendmail(msg['From'],msg['To'],msg.as_string())
    server.quit()
